package type.person;

import java.util.ArrayList;

/**
 * @Date 2021/5/5 10:30 上午
 * @Author dengxin
 */
public class Staff extends ArrayList<Position> {

    public void add(String title, Person person) {
        this.add(new Position(title, person));
    }

    public void add(String... titles) {
        for(String title : titles) {
            this.add(new Position(title, Person.NULL));
        }
    }

    public Staff(String... titles) {
        add(titles);
    }

    public boolean positionAvailable(String title) {
        for(Position position : this) {
            if (title.equals(position.getTitle()) && position.getPerson() == Person.NULL) {
                return true;
            }
        }
        return false;
    }

    public void fillPerson(String title, Person hire) {
        for (Position position : this) {
            if (title.equals(position.getTitle()) && position.getPerson() == Person.NULL) {
                position.setPerson(hire);
                return;
            }
        }
        throw new RuntimeException("Position " + title + "is not available");
    }

    public static void main(String[] args) {
        Staff staff = new Staff("President", "CTO", "CFO", "COO", "Project leader", "Engineer");
        staff.fillPerson("President", new Person("Herry", "Jack", "Star Road 111"));
        staff.fillPerson("CTO", new Person("Loraus", "Lee", "Moon River 123"));
        if (staff.positionAvailable("COO")) {
            staff.fillPerson("COO", new Person("Mark", "Hu", "Sun Village 333"));
        }
        System.out.println(staff);
    }
}
